package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.model.FrontInfor;
import com.rongke.service.FrontInforService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @FrontInforController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/frontInfor")
@Transactional
public class FrontInforController {
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private FrontInforService frontInforService;

	/**
	 * @根据id查找
	 * @param id
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.GET)
	public JsonResp selectFrontInfor(Long id) {
		log.debug("selectFrontInfor, id=" + id);
		try {
			FrontInfor frontInfor = frontInforService.selectById(id);
			if (frontInfor.isDelete()) {
				return JsonResp.ok();
			}
			return JsonResp.ok(frontInfor);
		} catch (Exception e) {
			log.error("selectFrontInfor failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @获取前沿资讯列表（分页）
	 * @param page
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectPage", method = RequestMethod.GET)
	public JsonResp selectFrontInfor(Page page) {
		log.debug("selectFrontInfor");

		try {
			EntityWrapper<FrontInfor> ew = new EntityWrapper<>();
			ew.eq("is_delete", false);
			ew.orderBy("is_top", false);
			ew.orderBy("gmt_modified", false);
			Page<FrontInfor> inforPage = frontInforService.selectPage(page, ew);
			return JsonResp.ok(inforPage);
		} catch (Exception e) {
			log.error("selectFrontInfor failed", e);
			return JsonResp.fa(null);
		}
	}

}
